﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using BackupLibrary;
using DevExpress.XtraEditors;


namespace BackupJob
{
    public partial class ProgramSettings : XtraForm
    {
        public ProgramSettings()
        {
            InitializeComponent();
        }

        private void SaveBtn_Click(object sender, EventArgs e)
        {
            if (BackupDirectoryTxt.Text == "" || emailAddressTxt.Text == "" || messageTxt.Text == "" || serverAddressTxt.Text == "" || userNameTxt.Text == "" || passwordTxt.Text == "" || txtMailFrom.Text == "")
            {
                MessageBox.Show("Bütün alanları doldurunuz.");
                return;
            }

            try
            {
                if (serviceController1.Status != System.ServiceProcess.ServiceControllerStatus.Running)
                {
                    serviceController1.Stop();
                }
                Settings.Get.WinrarPath = BackupDirectoryTxt.Text;
                Settings.Get.EmailAdress = emailAddressTxt.Text;
                Settings.Get.Message = messageTxt.Text;
                Settings.Get.ServerAddress = serverAddressTxt.Text;
                Settings.Get.UserId = userNameTxt.Text;
                Settings.Get.Password = passwordTxt.Text;
                Settings.Get.AccountType = AccountCmb.SelectedItem.ToString();
                Settings.Get.SenderMailAdress = txtMailFrom.Text;
                Settings.Get.SaveAll();

                if (serviceController1.Status == System.ServiceProcess.ServiceControllerStatus.Stopped)
                {
                    serviceController1.Start();
                }

                this.DialogResult = System.Windows.Forms.DialogResult.OK;
                this.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Ayarlar kaydedilemedi ! \r\nHata mesajı:" + ex.Message);
            }
        }

        private void ProgramSettings_Load(object sender, EventArgs e)
        {
            serviceController1.MachineName = Environment.MachineName;
            serviceController1.ServiceName = "SqlBackupService";

            BackupDirectoryTxt.Text = Settings.Get.WinrarPath;
            emailAddressTxt.Text = Settings.Get.EmailAdress;
            messageTxt.Text = Settings.Get.Message;
            serverAddressTxt.Text = Settings.Get.ServerAddress;
            userNameTxt.Text = Settings.Get.UserId;
            passwordTxt.Text = Settings.Get.Password;
            AccountCmb.Text = Settings.Get.AccountType;
            txtMailFrom.Text = Settings.Get.SenderMailAdress;
        }

        private void BackupDirectoryTxt_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog();
            dlg.FileName = "";
            if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                BackupDirectoryTxt.Text = dlg.FileName;
            }
        }

    }
}
